package com.ziguang.redwood.admin.controller;

import java.io.File;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.ziguang.common.SysConstant;
import com.ziguang.common.interceptor.AuthInterceptor;
import com.ziguang.redwood.admin.service.ProductService;
import com.ziguang.redwood.admin.validator.ProductValidatro;

@ControllerBind(controllerKey = "/product", viewPath = "/admin")
@Before(AuthInterceptor.class)
public class ProductController extends Controller {
	private static final Logger log  = LoggerFactory.getLogger(ProductController.class);
	
	/**
	 * 获取产品列表
	 */
	public void index(){
		List<Record> list = ProductService.me.findProduct();
		setAttr("data", list);
		render("product.html");
	}
	
	/**
	 * 编辑产品信息
	 */
	public void edit(){
		String id = this.getPara("id");
		if(id != ""){
			Record data = ProductService.me.QueryEdit(id);
			List<Record> list = ProductService.me.findCategory();
			setAttr("category", list);
			setAttr("data", data);
			render("product_edit.html");
		}else{
			List<Record> list = ProductService.me.findCategory();
			setAttr("category", list);
			render("product_edit.html");
		}
		
	}
	
	/**
	 * 保存产品信息
	 */
	public void save(){
		String sqlBigImg = "";
		String sqlSmallImg = "";
		// 文件上传
		String RootPath = SysConstant.UPLOAD_ROOT_PATH;
		String imgRootPath = getSessionAttr(SysConstant.SESSION_INFO);
		
		String imgBSPath = imgRootPath + File.separator + System.currentTimeMillis();
		File file = new File(RootPath+"product"+File.separator+imgBSPath+File.separator+"Big");
		File file1 = new File(RootPath+"product"+File.separator+imgBSPath+File.separator+"Small");
		UploadFile imgBig = getFile("pic", imgBSPath);
		UploadFile imgSmall = getFile("pic1", imgBSPath);
		if(imgBig != null){
			if(!file.exists()){
				file.mkdirs();
			}
			String bigImgName = imgBig.getFileName();
			String bigImg = RootPath+"product"+File.separator+imgBSPath+File.separator+"Big"+File.separator+bigImgName;
			imgBig.getFile().renameTo(new File(bigImg));
			sqlBigImg = imgBSPath+File.separator+"Big"+File.separator+bigImgName;
		}else{
			sqlBigImg = this.getPara("imgFix");
		}
		
		if(imgSmall != null){
			if(!file1.exists()){
				file1.mkdirs();
			}
			String smallImgName = imgSmall.getFileName();
			String smallImg = RootPath+"product"+File.separator+imgBSPath+File.separator+"Small"+File.separator+smallImgName;
			imgSmall.getFile().renameTo(new File(smallImg));
			sqlSmallImg = imgBSPath+File.separator+"Small"+File.separator+smallImgName;
		}else{
			sqlSmallImg = this.getPara("imgFix1");
		}
		
		String id = this.getPara("id");
		String productName = this.getPara("product_name");
		String productInfo = this.getPara("product_info");
		String typeId = this.getPara("type_id");
		String sort = this.getPara("sort");
		
		ProductService.me.save(id,productName,productInfo,sqlSmallImg,sqlBigImg,typeId,sort);
		index();
	}
	
	/**
	 * 删除当前产品
	 */
	public void delete(){
		String id = this.getPara("id");
		ProductService.me.delete(id);
		renderJson("{\"code\":0}");
	}
	
}
